<?php

class aria2ClientPlugin extends PluginBase
{
	function __construct()
	{
		parent::__construct();
	}
	public function regist()
	{
		$this->hookRegist(array(
			'user.commonJs.insert' => 'aria2ClientPlugin.echoJs',
		));
	}
	public function echoJs()
	{
		$this->echoFile('static/main.js');
	}

	public function index()
	{
		include($this->pluginPath . 'static/index.html');
	}

	public function config()
	{
		echo "<script>";
		$config = $this->getConfig();
		$this->echoFile('static/config.js', [
			'$rpcHost' => $config["rpcHost"],
			'$rpcPort' => $config["rpcPort"],
			'$rpcInterface' => $config["rpcInterface"],
			'$rpcProtocol' => $config["rpcProtocol"],
			'$rpcSecret' => $config["rpcSecret"] ? base64_encode($config["rpcSecret"]) : ""
		]);
		echo "</script>";
	}

	public function extScript()
	{
		$path = $this->in['path'];
		$name = $this->in['name'];
		$host = $this->config['APP_HOST_LINK'];
		$staticPath = $this->config['settings']['staticPath'];

		echo '<script>window.kodSdkConfig={api: "' . $host . '"};</script>';
		echo '<script src="' . $staticPath . 'app/dist/sdk.js"></script>';

		if ($path) {
			echo "<script>";
			$fileInfo = IO::info($path);
			if ($fileInfo) {
				$this->echoFile('static/startTorrent.js', array(
					"TORRENT_BASE64" => base64_encode(IO::fileSubstr($path, 0, $fileInfo["size"])),
					"TORRENT_NAME" => $name
				));
			}
			echo "</script>";
		}
	}
}
